﻿<UserControl x:Class="Samurai.WorkflowGraph.Views.WorkflowGraphView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:Views="clr-namespace:Samurai.WorkflowGraph.Views"
             xmlns:primitives="clr-namespace:Telerik.Windows.Controls.Diagrams.Primitives;assembly=Telerik.Windows.Controls.Diagrams"
             xmlns:Selectors="clr-namespace:Samurai.WorkflowGraph.Selectors" mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <UserControl.Resources>
        <Style TargetType="telerik:RadDiagram">
            <Setter Property="AllowCopy" Value="False" />
            <Setter Property="AllowCut" Value="False" />
            <Setter Property="AllowDelete" Value="False" />
            <Setter Property="AllowPaste" Value="False" />
            <Setter Property="IsConnectorsManipulationEnabled" Value="False" />
            <Setter Property="IsEditable" Value="False" />
            <Setter Property="IsResizingEnabled" Value="False" />
            <Setter Property="IsRotationEnabled" Value="False" />
            <Setter Property="IsSnapEnabled" Value="False" />
            <Setter Property="ActiveTool" Value="PointerTool" />
        </Style>
        <telerik:BooleanToVisibilityConverter x:Key="Converter" />
        <LinearGradientBrush x:Key="WorkflowStepBackgroundBrush" EndPoint="0.5,1" StartPoint="0.5,0">
            <GradientStop Color="#FFD6D6D6"/>
            <GradientStop Color="#FF939393" Offset="0.547"/>
            <GradientStop Color="#FFCECECE" Offset="1"/>
        </LinearGradientBrush>
        <Style TargetType="telerik:RadDiagramShape">
            <Setter Property="Position" Value="{Binding Position, Mode=TwoWay}" />
            <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
            <Setter Property="Padding" Value="0" />
        </Style>
        <Style TargetType="telerik:RadDiagramConnection">
            <Setter Property="Visibility" Value="{Binding Visibility, Mode=TwoWay}" />
            <Setter Property="SourceConnectorPosition" Value="Bottom" />
            <Setter Property="TargetConnectorPosition" Value="Top" />
            <Setter Property="BorderBrush" Value="#FF9F9E9E" />
            <Setter Property="ContentTemplate">
                <Setter.Value>
                    <DataTemplate />
                </Setter.Value>
            </Setter>
        </Style>

        <Storyboard x:Key="Collapse">
            <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0" BeginTime="0:0:0" Duration="0:0:0.5" />
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:0.5">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Collapsed</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
        </Storyboard>
        <Storyboard x:Key="Show">
            <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
                <DiscreteObjectKeyFrame KeyTime="0:0:0">
                    <DiscreteObjectKeyFrame.Value>
                        <Visibility>Visible</Visibility>
                    </DiscreteObjectKeyFrame.Value>
                </DiscreteObjectKeyFrame>
            </ObjectAnimationUsingKeyFrames>
            <DoubleAnimation Storyboard.TargetProperty="Opacity" To="1" BeginTime="0:0:0" Duration="0:0:0.5" />
        </Storyboard>

        <Style TargetType="TextBlock" x:Key="TextBlockNameStyle">
            <Setter Property="FontFamily" Value="Segoe UI Light" />
            <Setter Property="FontSize" Value="19" />
            <Setter Property="Foreground" Value="#FFFFFFFF" />
            <Setter Property="TextWrapping" Value="Wrap" />
            <Setter Property="HorizontalAlignment" Value="Left" />
        </Style>
        <Style TargetType="TextBlock" x:Key="TextBlockStyle">
            <Setter Property="FontFamily" Value="Segoe UI" />
            <Setter Property="FontSize" Value="11" />
            <Setter Property="Foreground" Value="#FF333333" />
        </Style>
        <Style TargetType="TextBlock" x:Key="TextBlockPositionStyle" BasedOn="{StaticResource TextBlockStyle}">
            <Setter Property="FontWeight" Value="Bold" />
            <Setter Property="Margin" Value="0 5 0 0" />
        </Style>
        <Style TargetType="telerik:RadToggleButton" x:Key="RadToggleButtonStyle">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="telerik:RadToggleButton">
                        <Grid Background="Transparent" Cursor="Hand">
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualState x:Name="Normal" />
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="path1"
													Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <SolidColorBrush Color="#FF333333" />
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                            <ObjectAnimationUsingKeyFrames Storyboard.TargetName="path2"
													Storyboard.TargetProperty="Fill">
                                                <DiscreteObjectKeyFrame KeyTime="0">
                                                    <DiscreteObjectKeyFrame.Value>
                                                        <SolidColorBrush Color="#FF333333" />
                                                    </DiscreteObjectKeyFrame.Value>
                                                </DiscreteObjectKeyFrame>
                                            </ObjectAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed" />
                                </VisualStateGroup>
                                <VisualStateGroup x:Name="CheckStates">
                                    <VisualState x:Name="Checked">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="arrow"
													Storyboard.TargetProperty="(FrameworkElement.RenderTransform).Angle"
													To="180" Duration="0:0:0.2" />
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Unchecked">
                                        <Storyboard>
                                            <DoubleAnimation Storyboard.TargetName="arrow"
													Storyboard.TargetProperty="(FrameworkElement.RenderTransform).Angle"
													To="0" Duration="0:0:0.2" />
                                        </Storyboard>
                                    </VisualState>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                            <Grid Height="9" Margin="0,2,0,0" Width="8" x:Name="arrow" RenderTransformOrigin="0.5,0.5">
                                <Path x:Name="path1"
										Data="M8,0.14471819 L8,1.999831 L4,4.2555118 L0,1.9998311 L0,0.1447183 L4,2.400399 z"
										Fill="White" Margin="0,4,0,0" Stretch="Fill" StrokeThickness="2"
										UseLayoutRounding="False" />
                                <Path x:Name="path2"
										Data="M8,0.14471819 L8,1.999831 L4,4.2555118 L0,1.9998311 L0,0.1447183 L4,2.400399 z"
										Fill="White" Margin="0,0,0,4" Stretch="Fill" StrokeThickness="2"
										UseLayoutRounding="False" />
                                <Grid.RenderTransform>
                                    <RotateTransform Angle="0" />
                                </Grid.RenderTransform>
                            </Grid>
                        </Grid>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
        
        <DataTemplate x:Key="StartNodeTemplate">
            <Grid Width="130" Height="60" Background="Transparent">
                <Ellipse Width="130" Height="60" Fill="Green" Stroke="DarkGreen" Opacity=".7" />

                <TextBlock Text="Start Workflow" Foreground="White" FontSize="14" HorizontalAlignment="Center" VerticalAlignment="Center" />
            </Grid>
        </DataTemplate>

        <DataTemplate x:Key="DelegatedStepNodeTemplate">
            <Grid Width="150" Height="50">
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="30" />
                    <ColumnDefinition Width="*" />
                    <ColumnDefinition Width="50" />
                </Grid.ColumnDefinitions>
                <Grid.RowDefinitions>
                    <RowDefinition Height="25" />
                    <RowDefinition Height="*" />
                </Grid.RowDefinitions>

                <Border BorderBrush="DimGray" BorderThickness="1" CornerRadius="3" Grid.ColumnSpan="3" Grid.RowSpan="2" Background="{StaticResource WorkflowStepBackgroundBrush}" />

                <Image Source="/Samurai.WorkflowGraph;component/Assets/Images/Decision-25.png" 
                    Visibility="{Binding IsConditional, Converter={StaticResource Converter}}" 
                    Grid.Column="0" Grid.Row="0" Margin="0,0,6,0" Width="20" />
                

                <Image Source="/Samurai.WorkflowGraph;component/Assets/Images/Critical-20.png" 
                    Visibility="{Binding IsCritical, Converter={StaticResource Converter}}"  Margin="0,0,6,0"
                    Grid.Column="0" Grid.Row="2" Height="20" Width="20" />

                <TextBlock Text="{Binding StepNameFormatted}" FontSize="14" Foreground="White" VerticalAlignment="Center" HorizontalAlignment="Center" Grid.ColumnSpan="3" Grid.RowSpan="2" />

                <telerik:RadToggleButton Visibility="{Binding HasChildren, Converter={StaticResource Converter}}" HorizontalAlignment="Right"
						Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Views:WorkflowGraphView}}, Path=DataContext.ToggleVisibilityCommand}"
						CommandParameter="{Binding}" VerticalAlignment="Top" Width="15" Height="15"
						Margin="0 5 5 0" Style="{StaticResource RadToggleButtonStyle}" Grid.Row="0" Grid.Column="2" />
                
                <Image Source="C:\Projects\Wakizashi\Samurai.WorkflowGraph\Assets\Images\Lambda-20.png" 
                    Height="20" Width="20" Margin="5" Grid.Row="1" Grid.Column="2"
                    VerticalAlignment="Center" HorizontalAlignment="Right" Opacity=".5" />                
            </Grid>
        </DataTemplate>

        <Selectors:ShapeTemplateSelector x:Key="ShapeTemplateSelector" 
            WorkflowStartNodeTemplate="{StaticResource StartNodeTemplate}"
            DelegatedStepNodeTemplate="{StaticResource DelegatedStepNodeTemplate}"/>

        <Style x:Key="ConnectionStyle" TargetType="telerik:RadDiagramConnection">
            <Setter Property="Content" Value="{Binding Content}" />
            <Setter Property="TargetCapType" Value="{Binding TargetCapType}" />
            <Setter Property="SourceCapType" Value="{Binding SourceCapType}" />
        </Style>
        
        <Style TargetType="telerik:RadDiagramShape" x:Key="ShapeStyle">
            <Setter Property="Geometry" Value="{x:Null}" />
        </Style>
    </UserControl.Resources>
    <Grid Background="Transparent">
        <telerik:RadBusyIndicator Grid.Row="1" BusyContent="{Binding BusyContent}"  IsIndeterminate="True">
            <Border BorderBrush="#FFD6D4D4" Grid.Row="1" BorderThickness="0 0 1 1" Background="White" Margin="0 4 0 0">
                <telerik:RadDiagram x:Name="WorkflowDiagram" GraphSource="{Binding GraphSource, Mode=OneWay}" ConnectionStyle="{StaticResource ConnectionStyle}" SizeChanged="OnDiagramSizeChanged"
					ShapeTemplateSelector="{StaticResource ShapeTemplateSelector}" SelectionMode="Extended" ShapeStyle="{StaticResource ShapeStyle}" />
            </Border>
        </telerik:RadBusyIndicator>
    </Grid>
</UserControl>
